﻿using Blm.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsTaskReportRepository : BaseRepository<MsTaskReport>, IMsTaskReportRepository
    {
        /// <summary>
        /// 根据fileid和机器人的token,id生成报表文件数据
        /// </summary>
        public void AddorUpdateReport(Guid taskId, string filePath, Guid operatorID)
        {
            MsTaskReport report = Db.MsTaskReport.AsNoTracking().Where(q => q.TaskId == taskId).Select(i => i).FirstOrDefault();

            if (report != null)
            {
                this.Update(t => t.TaskId == taskId, (Action<MsTaskReport>)(d =>
                {
                    d.ReportPath = filePath;
                    d.LastUpdatedDate = DateTime.Now;
                    d.LastUpdatedBy = operatorID;
                    d.IsNew = true;
                    d.IsDeleted = false;
                    d.SyncStatus = "0";
                }));
                // report.ReportPath = filePath;
                // this.Update(report, w => w.ReportPath&&w.LastUpdatedDate=DateTime.Now);
            }
            else
            {
                //查找项目ID
                MsTask task = Db.MsTasks.Where(q => q.TaskId == taskId).Select(i => i).FirstOrDefault();

                MsTaskReportDto reportDto = new MsTaskReportDto();

                reportDto.FileId = Guid.NewGuid();
                reportDto.ReportPath = filePath;
                var entity = reportDto.CopyTo<MsTaskReport>();
                entity.TaskReportId = Guid.NewGuid();
                entity.ReportName = task.TaskName;
                entity.TaskId = taskId;
                //    entity.ReportName= Path.GetFileNameWithoutExtension(filePath);
                entity.IsDeleted = false;
                entity.ReportType = 1;
                entity.ProjectId = task.ProjectId;
                entity.CreatedDate = DateTime.Now;
                entity.LastUpdatedDate = DateTime.Now;
                entity.IsNew = true;
                entity.CreatedBy = operatorID;
                entity.LastUpdatedBy = operatorID;
                entity.IsDeleted = false;
                this.Add(entity);
            }
        }

        /// <summary>
        /// 更新报表状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task UpdateReport(MsTaskReportDto model)
        {
            MsTaskReport report = Db.MsTaskReport.AsNoTracking().Where(q => q.TaskReportId == model.TaskReportId).Select(i => i).FirstOrDefault();

            if (report != null)
            {
                report.IsNew = model.IsNew;
                this.Update(report, x => new { x.IsNew, x.LastUpdatedBy, x.LastUpdatedDate });
            }
        }

        public async Task<MsTaskReport> GetTaskReportForTaskId(Guid taskId)
        {
            MsTaskReport report = await Db.MsTaskReport.FirstOrDefaultAsync(r => r.TaskId == taskId);
            return report;
        }
    }
}